import {verifyQrInfo,bindWorkUser} from '@/common/api/order.js'
import {showToast,showErrorModal} from '@/common/utils/util.js'
import {getAppDictConfig} from '@/common/api/index_api.js'
export default {
	namespaced: true,
	state: () => ({
    mode:'', // 'info','booking','order','comment'  order:通过订单内的预约二维码
    commonId: '', // 不同情况代表不同id 此id可能是 咨询订单预约id、咨询订单评分id 具体根据mode的变化
    postKey: '',
    qrCodeInfo:{
      userId:'',
      isBind:false
    },
    activitySwitch:false,
    sclFreeMenuSwitch:false,
    dict:{
      eshopChannelDatas:[],
      otherConsultLabels:[]
    },
    isAuditMode:true,
    isCloseDepression:true,
    showExplanation: false
	}),
  getters: {
  	eshopChannelDatas(state) {
  		return state.dict.eshopChannelDatas
  	},
    otherConsultLabels(state){
      return state.dict.otherConsultLabels
    }
  },
	mutations: {
    setIsAuditMode(state,data){
      state.isAuditMode = data
    },
    setIsCloseDepression(state,data){
      state.isCloseDepression = data
    },
    setShowExplanation(state,data){
      state.showExplanation = data
    },
    setSclFreeMenuSwitch(state,data){
      state.sclFreeMenuSwitch = data
    },
    setDict(state,data){
      state.dict = data
    },
    setActivitySwitch(state,data){
      state.activitySwitch = data=='Y' ? true : false
    },
    setQrCodeInfo(state,data){
      state.qrCodeInfo = {
        userId:data.userId,
        isBind:data.isBind
      }
    },
    setCommonId(state,data){
      state.commonId = data
    },
    setMode(state,data){
      state.mode = data
    },
    setPostKey(state,data){
      state.postKey = data
    },
    setScanParams(state,data){
      console.log('setScanParams - data：',data)
      
      this.commit("app/setMode",data.mode)
      this.commit('app/setCommonId',data.commonId)
      this.commit('app/setPostKey',data.postKey)    
    },
    clearScanParams(state){
      this.commit("app/setMode",'')
      this.commit('app/setCommonId','')
      this.commit('app/setPostKey','')    
    }
	},
	actions: {
    getAppDictConfig({commit}){
      // commit('setAiEntrance',res.data)
      return Promise.all([
        getAppDictConfig().then(({data:res})=>{
          console.log(res,'========res====')
          if(res.success){
            commit('setIsAuditMode',res.data == 'Y')
          }else{
            commit('setIsAuditMode',true)
          }
        }).catch(()=>{
          commit('setIsAuditMode',true)
        }),
        getAppDictConfig({
          code:'close_depression'
        }).then(({data:res})=>{
          console.log(res,'========res====1')
          if(res.success){
            commit('setIsCloseDepression',res.data == 'Y')
          }else{
            commit('setIsCloseDepression',true)
          }
        }).catch(()=>{
          commit('setIsCloseDepression',true)
        }),
        getAppDictConfig({
          code: 'unscramble_close'
        }).then(res => {
          if (res.data.success && res.data.data === 'Y') {
            commit('setShowExplanation', true)
          } else {
            commit('setShowExplanation', false)
          }
        })
      ])
    },
    checkScanParamsToAction({state}){
      if(state.mode == ''){
        console.log('scanParams is null,error')
        
        uni.reLaunch({
          url: '/pages/sclList/sclList'
        })
        
        return false
      }
      const { postKey,commonId,mode } = state
      
      if(mode == 'info'){
        setTimeout(()=>{
          uni.reLaunch({
            // url:'/subcontractorB/pages/order/order'
            url:'/pages/consultAccount/consultAccount'
          })
        },1000)
      }else if(mode == 'booking' || mode == 'order'){
        uni.redirectTo({
          url:'/subcontractorB/pages/order/detail'
        })
      }else if(mode == 'comment'){
        // uni.redirectTo({
        //   url:'/subcontractorB/pages/order/comment'
        // })
		uni.redirectTo({
		  url:'/subcontractorC/pages/returnVisit/returnVisit?orderUid='+commonId
		})
      }else if(mode == 'pay'){
        uni.redirectTo({
          url:'/subcontractorA/payment/details'
        })
      }else{
        uni.reLaunch({
          url: '/pages/sclList/sclList'
        })
      }
    },
    async getQrcodeInfo({commit,state,rootState}){
      if(state.mode != 'info' && state.mode != 'booking'){
        return false
      }    
      // console.log(rootState,'---rootState')
      //如果mode不为空 则说明是扫码进入的，需要执行扫码进入的逻辑
      const { postKey,commonId } = state
      
      try{
        const {data:res} = await verifyQrInfo({[postKey]: commonId})
        const {isBind,userId} = res.data
        commit('setQrCodeInfo',res.data)
        if(rootState.user.userInfo?.userUid){
          if(userId === rootState.user.userInfo.userUid){
            console.log('已绑定该用户，无需重复绑定')
            showToast('已绑定该用户，无需重复绑定')
          }else{
            showErrorModal('无权查看，请联系客服')
          }
        }else if(!isBind){
          const {data:res} = await bindWorkUser({userId})
          if(res.success){
            showToast('绑定成功')
          }else{
            showToast('绑定失败',res.message)
          }
        }else{
          showErrorModal('该用户已被绑定，请联系客服')
        }
      }catch(e){
        showToast(JSON.stringify(e));
      }
    },
	}
}
